package com.jit.monitor.presenter;

import android.annotation.SuppressLint;

import com.jit.baselib.bean.response.BaseResponse;
import com.jit.baselib.ui.base.BaseActivity;
import com.jit.baselib.ui.base.BasePresenter;
import com.jit.monitor.base.ApiRetrofit;
import com.jit.monitor.commom.AppConstant;
import com.jit.monitor.view.EditCmNameAtView;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author crazyZhangxl on 2018/12/21.
 * Describe:
 */
public class EditCmAtPresenter extends BasePresenter<EditCmNameAtView>{

    public EditCmAtPresenter(BaseActivity context) {
        super(context);
    }

    @SuppressLint("CheckResult")
    public void submitCameraName(String cameraId, String cameraName){
        getView().showLoadingDialog();;
        ApiRetrofit.getInstance().editCameraName(cameraId,cameraName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResponse>() {
                    @Override
                    public void accept(BaseResponse baseResponse) throws Exception {
                        getView().hideLoadingDialog();
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            getView().editNameSuccess();
                        }else {
                            getView().editNameFailure(baseResponse.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().hideLoadingDialog();
                        getView().editNameFailure(throwable.getLocalizedMessage());
                    }
                });
    }
}
